var file_upload_adver = {
	btnUpload : {},
	btnDelete : {},
	status : {},
	options: {
		action: '/uploada.php',//Куда будет слаться файл
		name: 'adver',
		btnUpload: '#sl_dl_form_im',
		btnDelete: 'div.close_menu',
		allowedExtensions:['jpeg', 'jpg', 'png'],
	},
	init: function(opt) {
		file_upload_adver.options = $.extend(file_upload_adver.options , opt);
		file_upload_adver.btnUpload = $(file_upload_adver.options.btnUpload);
		file_upload_adver.status = $(file_upload_adver.options.status);
		_this = this;
		$(function() {
			var options = file_upload_adver.options;
			new AjaxUpload(file_upload_adver.btnUpload, {
				action: options.action,
				name: options.name,
				onSubmit: function(file, ext) {
					if (!(ext && /^(?:jpg|jpeg|png)$/i.test(ext))) {
						file_upload_adver.status.text('');
						return false;
					}
					$('#files').append('<div class="sl_ajax_img sl_ajax_loading" style="width:128px;height:138px"><div class="sl_ajax_set"></div><img class="img_in_form" src="" alt=""><div class="close_menu" onclick="file_upload_adver.delete_file.call(this)"></div></div>');
				},
				onComplete: function (file, response) {
					file_upload_adver.btnUpload.value = response['name'];
					var response = jQuery.parseJSON(response);
					$('#image').val(response.msg2);
					$('.img_in_form').last().attr('src', response.msg2).end().parents().find('.sl_ajax_img').removeClass('sl_ajax_loading').removeAttr('style');
					$('#files_storage').append('<input type="hidden" name="images[]" value="'+ response.msg2 +'">');
				}
			});
		});
	},
	delete_file: function() {
		var file = $(this).parents().find('img').first().attr('src');
		var skop = $(this).parents('.sl_ajax_img').first();
		CORE.Ajax({
				module: 'adver',
				language: CORE.language,
				file_name: file,
				event: 'delete_file'
			},
			function(res) {
				$(skop).remove();
			}
		);
	}
};

$(function() {
	if ($(file_upload_adver.options.btnUpload).length) {
		file_upload_adver.init();
	}
});